package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.ArrayList;

public class RoleOperation_CreateRoleReturnProto implements IProto {
    private static MMO_DataStream dataStream = new MMO_DataStream();
    @Override
    public short getProtoCode() {
        return 10004;
    }

    public Boolean IsSuccess; //是否成功
    public short MessageId; //错误编号

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteBool(IsSuccess);
            if(!IsSuccess)
            {
                dataStream.WriteShort(MessageId);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static RoleOperation_CreateRoleReturnProto GetProto(byte[] buffer)
    {
        RoleOperation_CreateRoleReturnProto proto = new RoleOperation_CreateRoleReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.IsSuccess = dataStream.ReadBool();
            if (!proto.IsSuccess) {
                proto.MessageId = dataStream.ReadShort();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}
